const cloud = require('wx-server-sdk')
cloud.init()
const db = cloud.database()

exports.main = async (event, context) => {
  const { OPENID } = cloud.getWXContext()

  try {
    const res = await db.collection('lotteryData').where({ _openid: OPENID }).get()
    if (res.data.length > 0) {
      return {
        code: 0,
        msg: '查询成功',
        data: res.data[0]
      }
    } else {
      return {
        code: 1,
        msg: '无数据记录',
        data: null
      }
    }
  } catch (err) {
    return {
      code: 500,
      msg: '查询失败',
      error: err
    }
  }
}
